package com.horizon.manage.site.service.impl;

import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.horizon.common.constrants.HorizonResultCodeEnum;
import com.horizon.common.exception.HorizonException;
import com.horizon.manage.site.converter.SiteEvdConverter;
import com.horizon.manage.site.dto.SiteDTO;
import com.horizon.manage.site.dto.SiteItemDTO;
import com.horizon.manage.site.entity.Site;
import com.horizon.manage.site.mapper.SiteMapper;
import com.horizon.manage.site.service.ISiteService;
import lombok.AllArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 场地service实现.
 *
 * @author zhaoshb
 * @since 2022/12/5 23:56
 */
@Service
@AllArgsConstructor
public class SiteServiceImpl extends BaseServiceImpl<SiteMapper, Site> implements ISiteService {

    private SiteEvdConverter siteEvdConverter;

    @Override
    public List<Long> querySiteSupportItemList(Long siteId) {
        return getBaseMapper().querySiteSupportItemList(siteId);
    }

    @Override
    @Transactional
    public Long saveSite(SiteDTO siteDTO) throws HorizonException {
        Site site = siteEvdConverter.dtoToEntity(siteDTO);
        boolean saved = save(site);
        if (!saved) {
            throw new HorizonException(HorizonResultCodeEnum.SITE_SAVE_FAILED);
        }
        getBaseMapper().deleteSiteItemBySiteId(site.getId());
        List<SiteItemDTO> siteItemDTOList = toSiteItemDTO(site.getId(), siteDTO.getSupportItemList());
        if (CollectionUtils.isNotEmpty(siteItemDTOList)) {
            getBaseMapper().batchInsertSiteItem(siteItemDTOList);
        }
        return site.getId();
    }

    @Override
    @Transactional
    public boolean deleteSite(Long siteId) {
        getBaseMapper().deleteSiteItemBySiteId(siteId);
        return SqlHelper.retBool(getBaseMapper().deleteById(siteId));
    }

    @Override
    @Transactional
    public boolean updateSite(SiteDTO siteDTO) throws HorizonException {
        Site site = siteEvdConverter.dtoToEntity(siteDTO);
        boolean saved = updateById(site);
        if (!saved) {
            throw new HorizonException(HorizonResultCodeEnum.SITE_NOT_EXIST);
        }
        getBaseMapper().deleteSiteItemBySiteId(site.getId());
        List<SiteItemDTO> siteItemDTOList = toSiteItemDTO(site.getId(), siteDTO.getSupportItemList());
        if (CollectionUtils.isNotEmpty(siteItemDTOList)) {
            getBaseMapper().batchInsertSiteItem(siteItemDTOList);
        }
        return true;
    }

    private List<SiteItemDTO> toSiteItemDTO(Long siteId, List<Long> itemIdList) {
        if (CollectionUtils.isNotEmpty(itemIdList)) {
            return Collections.EMPTY_LIST;
        }
        List<SiteItemDTO> dtoList = new ArrayList<>();
        for (Long itemId : itemIdList) {
            dtoList.add(new SiteItemDTO(siteId, itemId));
        }
        return dtoList;
    }
}
